﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

namespace CPF_experement
{
    // bla bla
    static class Run
    {
        static public Stopwatch watch;
        static public int row;
        static public bool trevor_A_Star;
        static public int A_Star_OutOfTime;
        static public int trevor_OutOfTime;

        public static void generateAndSolve(int gridSize, int agentsNum, int obsticalsNum, int instance, Excel.Application excelApp)
        {
            Console.WriteLine("\n\nGrid size - "+gridSize+"X"+gridSize);
            Console.WriteLine("Number Of Obsticals - " + obsticalsNum);
            Console.WriteLine("Number Of Agents - " + agentsNum);
            Console.WriteLine("Instance - " + instance);
            Console.WriteLine("---------------------------");

            trevor_A_Star = false;
            watch = new Stopwatch();
            AgentState[]  aStart = generateBoard(gridSize, agentsNum, obsticalsNum);

            Console.WriteLine("A star");
            Console.WriteLine("-------");
            excelApp.Cells[row, 1] = instance;
            excelApp.Cells[row, 2] = "A star";
            excelApp.Cells[row, 3] = gridSize + "X" + gridSize;
            excelApp.Cells[row, 4] = obsticalsNum;
            excelApp.Cells[row, 5] = agentsNum;
            if (A_Star_OutOfTime <= 10)
            {
                A_star.init_A_Star(aStart, false);
                watch.Start();
                A_star.solve();
                watch.Stop();
                excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
                excelApp.Cells[row, 7] = A_star.totalTime;
                excelApp.Cells[row, 8] = A_star.generated;
                excelApp.Cells[row, 9] = A_star.expanded;
                excelApp.Cells[row, 10] = 1;
                excelApp.Cells[row, 11] = 1;
                if (A_star.totalTime == -1)
                {
                    excelApp.Cells[row, 7] = "Fail";
                    excelApp.Cells[row, 6] = "Fail";
                    excelApp.Cells[row, 10] = 0;
                    excelApp.Cells[row, 11] = "irrelevant";
                }
                if (A_star.totalTime == -2)
                {
                    excelApp.Cells[row, 7] = "Out of time";
                    excelApp.Cells[row, 10] = 0;
                    excelApp.Cells[row, 11] = "N/A";
                    A_Star_OutOfTime++;
                }
                else
                {
                    A_Star_OutOfTime = 0;
                }
                Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
            }
            else
            {
                excelApp.Cells[row, 6] = "irrelevant";
                excelApp.Cells[row, 7] = "irrelevant";
                excelApp.Cells[row, 8] = "irrelevant";
                excelApp.Cells[row, 9] = "irrelevant";
                excelApp.Cells[row, 10] = 0;
                excelApp.Cells[row, 11] = "N/A";
                Console.WriteLine("irrelevant");
            }
            row++;

            Console.WriteLine("\ntrevor");
            Console.WriteLine("------");
            excelApp.Cells[row, 1] = instance;
            excelApp.Cells[row, 2] = "Trevor";
            excelApp.Cells[row, 3] = gridSize + "X" + gridSize;
            excelApp.Cells[row, 4] = obsticalsNum;
            excelApp.Cells[row, 5] = agentsNum;
            if (trevor_OutOfTime <= 10)
            {
                trevor_A_Star = true;
                Trevor t = new Trevor(aStart, false);
                watch.Reset();
                watch.Start();
                t.solve();
                watch.Stop();
                excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
                excelApp.Cells[row, 7] = Trevor.totalTime;
                excelApp.Cells[row, 8] = Trevor.generated;
                excelApp.Cells[row, 9] = Trevor.expanded;
                excelApp.Cells[row, 10] = 1;
                excelApp.Cells[row, 11] = 1;
                excelApp.Cells[row, 12] = t.min_group;
                excelApp.Cells[row, 13] = t.max_group;
                if (Trevor.totalTime == -1)
                {
                    excelApp.Cells[row, 7] = "Fail";
                    excelApp.Cells[row, 6] = "Fail";
                    excelApp.Cells[row, 10] = 0;
                    excelApp.Cells[row, 11] = "N/A";
                }
                if (Trevor.totalTime == -2)
                {
                    excelApp.Cells[row, 7] = "Out of time";
                    excelApp.Cells[row, 10] = 0;
                    excelApp.Cells[row, 11] = "N/A";
                    trevor_OutOfTime++;
                }
                else
                {
                    trevor_OutOfTime = 0;
                }
                Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
                trevor_A_Star = false;
            }
            else
            {
                excelApp.Cells[row, 6] = "irrelevant";
                excelApp.Cells[row, 7] = "irrelevant";
                excelApp.Cells[row, 8] = "irrelevant";
                excelApp.Cells[row, 9] = "irrelevant";
                excelApp.Cells[row, 10] = 0;
                excelApp.Cells[row, 11] = "N/A";
                excelApp.Cells[row, 12] = "N/A";
                excelApp.Cells[row, 13] = "N/A";
            }
            row++;

            Console.WriteLine("\nsilver");
            Console.WriteLine("------");
            Silver svr = new Silver(aStart, false);
            watch.Reset();
            watch.Start();
            svr.solve();
            watch.Stop();
            excelApp.Cells[row, 1] = instance;
            excelApp.Cells[row, 2] = "Silver";
            excelApp.Cells[row, 3] = gridSize + "X" + gridSize;
            excelApp.Cells[row, 4] = obsticalsNum;
            excelApp.Cells[row, 5] = agentsNum;
            excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
            excelApp.Cells[row, 7] = svr.totalTime;
            excelApp.Cells[row, 8] = svr.generated;
            excelApp.Cells[row, 9] = svr.expanded;
            excelApp.Cells[row, 10] = 1;
            if (svr.totalTime == -1)
            {
                excelApp.Cells[row, 7] = "Fail";
                excelApp.Cells[row, 6] = "Fail";
                excelApp.Cells[row, 10] = 0;
                excelApp.Cells[row, 11] = "N/A";
            }
            else
            {
                if (Trevor.totalTime != 0)
                    excelApp.Cells[row, 11] = (double)svr.totalTime / (double)Trevor.totalTime;
                else
                    excelApp.Cells[row, 11] = "N/A";
            }
            Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
            row++;

            Console.WriteLine("\nnew Algoritem");
            Console.WriteLine("----------------");
            SuperTree newAlg = new SuperTree(aStart);
            watch.Reset();
            watch.Start();
            newAlg.solve();
            watch.Stop();
            excelApp.Cells[row, 1] = instance;
            excelApp.Cells[row, 2] = "New algoritem";
            excelApp.Cells[row, 3] = gridSize + "X" + gridSize;
            excelApp.Cells[row, 4] = obsticalsNum;
            excelApp.Cells[row, 5] = agentsNum;
            excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
            excelApp.Cells[row, 7] = newAlg.totalCost;////
            excelApp.Cells[row, 8] = "N/A";
            excelApp.Cells[row, 9] = "N/A";
            excelApp.Cells[row, 10] = 1;
            if (svr.totalTime == -2)
            {
                excelApp.Cells[row, 7] = "Out of time";
                excelApp.Cells[row, 10] = 0;
                excelApp.Cells[row, 11] = "N/A";
            }
            else
            {
                if (Trevor.totalTime >= 0)
                    excelApp.Cells[row, 11] = (double)svr.totalTime / (double)Trevor.totalTime;
                else
                    excelApp.Cells[row, 11] = "N/A";
            }
            Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
            row++;

            if (Trevor.totalTime > -1 && newAlg.totalCost>-1 && Trevor.totalTime!=newAlg.totalCost)
            {
                Console.WriteLine("problem");
                Console.ReadLine();
            }
            Console.ReadLine();
        }

        public static AgentState[] generateBoard(int gridSize, int agentsNum, int obsticalsNum)
        {
            Random rand = new Random();
            int x;
            int y;
            Agent[] aGoals = new Agent[agentsNum];
            AgentState[] aStart = new AgentState[agentsNum];
            bool[][] grid = new bool[gridSize][];
            bool[][] goals = new bool[gridSize][];

            for (int i = 0; i < gridSize; i++)
            {
                grid[i] = new bool[gridSize];
                goals[i] = new bool[gridSize];
            }
            for (int i = 0; i < obsticalsNum; i++)
            {
                x = rand.Next(gridSize);
                y = rand.Next(gridSize);
                if (grid[x][y])
                    i--;
                grid[x][y] = true;
            }
            for (int i = 0; i < agentsNum; i++)
            {
                x = rand.Next(gridSize);
                y = rand.Next(gridSize);
                if (goals[x][y] || grid[x][y])
                    i--;
                else
                {
                    goals[x][y] = true;
                    aGoals[i] = new Agent(x, y, i);
                }
            }
            True_Path_Huristic.init(gridSize, gridSize, aGoals, false, grid);

            for (int i = 0; i < agentsNum; i++)
            {
                aStart[i] = new AgentState(aGoals[i].Goal_X, aGoals[i].Goal_Y, aGoals[i]);
            }

            int direction;

            for (int j = 0; j < 100000 ; j++)
            {
                for (int i = 0; i < agentsNum; i++)
                {
                    direction = rand.Next(0, 4);
                    switch (direction)
                    {
                        case 0:
                            if (True_Path_Huristic.isValidTile(aStart[i].pos_X - 1, aStart[i].pos_Y) && !goals[aStart[i].pos_X - 1][aStart[i].pos_Y])
                            {
                                goals[aStart[i].pos_X - 1][aStart[i].pos_Y] = true;
                                goals[aStart[i].pos_X][aStart[i].pos_Y] = false;
                                aStart[i].pos_X--;
                            }
                            break;
                        case 1:
                            if (True_Path_Huristic.isValidTile(aStart[i].pos_X, aStart[i].pos_Y + 1) && !goals[aStart[i].pos_X][aStart[i].pos_Y + 1])
                            {
                                goals[aStart[i].pos_X][aStart[i].pos_Y + 1] = true;
                                goals[aStart[i].pos_X][aStart[i].pos_Y] = false;
                                aStart[i].pos_Y++;
                            }
                            break;
                        case 2:
                            if (True_Path_Huristic.isValidTile(aStart[i].pos_X + 1, aStart[i].pos_Y) && !goals[aStart[i].pos_X + 1][aStart[i].pos_Y])
                            {
                                goals[aStart[i].pos_X + 1][aStart[i].pos_Y] = true;
                                goals[aStart[i].pos_X][aStart[i].pos_Y] = false;
                                aStart[i].pos_X++;
                            }
                            break;
                        case 3:
                            if (True_Path_Huristic.isValidTile(aStart[i].pos_X, aStart[i].pos_Y - 1) && !goals[aStart[i].pos_X][aStart[i].pos_Y - 1])
                            {
                                goals[aStart[i].pos_X][aStart[i].pos_Y - 1] = true;
                                goals[aStart[i].pos_X][aStart[i].pos_Y] = false;
                                aStart[i].pos_Y--;
                            }
                            break;
                    }
                }
            }
            for (int i = 0; i < agentsNum; i++)
            {
                aStart[i].setHuristic(0);
            }
            return aStart;
        }

        //public static void solveGivenGrid(bool[][] grid, int agentsNum, int instance, Excel.Application excelApp)
        //{

        //    Random rand = new Random();
        //    int x;
        //    int y;
        //    int gridSize = grid.Length;
        //    Agent[] aGoals = new Agent[agentsNum];
        //    AgentState[] aStart = new AgentState[agentsNum];
        //    bool[][] goals = new bool[grid.Length][];

        //    for (int i = 0; i < grid.Length; i++)
        //    {
        //        goals[i] = new bool[grid.Length];
        //    }

        //    for (int i = 0; i < agentsNum; i++)
        //    {
        //        x = rand.Next(10);
        //        y = rand.Next(10);
        //        if (goals[x][y] || grid[x][y])
        //            i--;
        //        else
        //        {
        //            goals[x][y] = true;
        //            aGoals[i] = new Agent(x, y, i);
        //        }
        //    }
        //    True_Path_Huristic.init(grid.Length, grid.Length, aGoals, false, grid);

        //    for (int i = 0; i < agentsNum; i++)
        //    {
        //        x = rand.Next(246, 256);
        //        y = rand.Next(246, 256);
        //        if (goals[x][y] || grid[x][y])
        //            i--;
        //        else
        //            aStart[i] = new AgentState(x, y, aGoals[i]);
        //    }




        //    trevor_A_Star = false;
        //    watch = new Stopwatch();

        //    Console.WriteLine("---------------------------");
        //    Console.WriteLine("Number Of Agents - " + agentsNum);
        //    Console.WriteLine("Instance - " + instance);
        //    Console.WriteLine("---------------------------");


        //    //Console.WriteLine("A star");
        //    //Console.WriteLine("-------");
        //    //excelApp.Cells[row, 1] = instance;
        //    //excelApp.Cells[row, 2] = "A star";
        //    //excelApp.Cells[row, 3] = gridSize + "X" + gridSize;

        //    //excelApp.Cells[row, 5] = agentsNum;
        //    //if (A_Star_OutOfTime <= 10)
        //    //{
        //    //    A_star.init_A_Star(aStart, false);
        //    //    watch.Start();
        //    //    A_star.solve();
        //    //    watch.Stop();
        //    //    excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
        //    //    excelApp.Cells[row, 7] = A_star.totalTime;
        //    //    excelApp.Cells[row, 8] = A_star.generated;
        //    //    excelApp.Cells[row, 9] = A_star.expanded;
        //    //    excelApp.Cells[row, 10] = 1;
        //    //    excelApp.Cells[row, 11] = 1;
        //    //    if (A_star.totalTime == -1)
        //    //    {
        //    //        excelApp.Cells[row, 7] = "Fail";
        //    //        excelApp.Cells[row, 6] = "Fail";
        //    //        excelApp.Cells[row, 10] = 0;
        //    //        excelApp.Cells[row, 11] = "irrelevant";
        //    //    }
        //    //    if (A_star.totalTime == -2)
        //    //    {
        //    //        excelApp.Cells[row, 7] = "Out of time";
        //    //        excelApp.Cells[row, 10] = 0;
        //    //        excelApp.Cells[row, 11] = "N/A";
        //    //        A_Star_OutOfTime++;
        //    //    }
        //    //    else
        //    //    {
        //    //        A_Star_OutOfTime = 0;
        //    //    }
        //    //    Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
        //    //}
        //    //else
        //    //{
        //    //    excelApp.Cells[row, 6] = "irrelevant";
        //    //    excelApp.Cells[row, 7] = "irrelevant";
        //    //    excelApp.Cells[row, 8] = "irrelevant";
        //    //    excelApp.Cells[row, 9] = "irrelevant";
        //    //    excelApp.Cells[row, 10] = 0;
        //    //    excelApp.Cells[row, 11] = "N/A";
        //    //    Console.WriteLine("irrelevant");
        //    //}
        //    //row++;

        //    //Console.WriteLine("\ntrevor");
        //    //Console.WriteLine("------");
        //    //excelApp.Cells[row, 1] = instance;
        //    //excelApp.Cells[row, 2] = "Trevor";
        //    //excelApp.Cells[row, 3] = gridSize + "X" + gridSize;

        //    //excelApp.Cells[row, 5] = agentsNum;
        //    //if (trevor_OutOfTime <= 10)
        //    //{
        //    //    trevor_A_Star = true;
        //    //    Trevor t = new Trevor(aStart, false);
        //    //    watch.Reset();
        //    //    watch.Start();
        //    //    t.solve();
        //    //    watch.Stop();
        //    //    excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
        //    //    excelApp.Cells[row, 7] = Trevor.totalTime;
        //    //    excelApp.Cells[row, 8] = Trevor.generated;
        //    //    excelApp.Cells[row, 9] = Trevor.expanded;
        //    //    excelApp.Cells[row, 10] = 1;
        //    //    excelApp.Cells[row, 11] = 1;
        //    //    excelApp.Cells[row, 12] = t.min_group;
        //    //    excelApp.Cells[row, 13] = t.max_group;
        //    //    if (Trevor.totalTime == -1)
        //    //    {
        //    //        return;
        //    //    }
        //    //    if (Trevor.totalTime == -2)
        //    //    {
        //    //        excelApp.Cells[row, 7] = "Out of time";
        //    //        excelApp.Cells[row, 10] = 0;
        //    //        excelApp.Cells[row, 11] = "N/A";
        //    //        trevor_OutOfTime++;
        //    //    }
        //    //    else
        //    //    {
        //    //        trevor_OutOfTime = 0;
        //    //    }
        //    //    Console.WriteLine("smallest group: {0}", t.min_group);
        //    //    Console.WriteLine("biggest group: {0}", t.max_group);
        //    //    Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
        //    //    trevor_A_Star = false;
        //    //}
        //    //else
        //    //{
        //    //    excelApp.Cells[row, 6] = "irrelevant";
        //    //    excelApp.Cells[row, 7] = "irrelevant";
        //    //    excelApp.Cells[row, 8] = "irrelevant";
        //    //    excelApp.Cells[row, 9] = "irrelevant";
        //    //    excelApp.Cells[row, 10] = 0;
        //    //    excelApp.Cells[row, 11] = "N/A";
        //    //    excelApp.Cells[row, 12] = "N/A";
        //    //    excelApp.Cells[row, 13] = "N/A";
        //    //}
        //    //row++;
        //    //Console.WriteLine("\nsilver");
        //    //Console.WriteLine("------");
        //    //Silver svr = new Silver(aStart, false);
        //    //watch.Reset();
        //    //watch.Start();
        //    //svr.solve();
        //    //watch.Stop();
        //    //excelApp.Cells[row, 1] = instance;
        //    //excelApp.Cells[row, 2] = "Silver";
        //    //excelApp.Cells[row, 3] = gridSize + "X" + gridSize;

        //    //excelApp.Cells[row, 5] = agentsNum;
        //    //excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
        //    //excelApp.Cells[row, 7] = svr.totalTime;
        //    //excelApp.Cells[row, 8] = svr.generated;
        //    //excelApp.Cells[row, 9] = svr.expanded;
        //    //excelApp.Cells[row, 10] = 1;
        //    //if (svr.totalTime == -1)
        //    //{
        //    //    excelApp.Cells[row, 7] = "Fail";
        //    //    excelApp.Cells[row, 6] = "Fail";
        //    //    excelApp.Cells[row, 10] = 0;
        //    //    excelApp.Cells[row, 11] = "N/A";
        //    //}
        //    //else
        //    //{
        //    //    if (Trevor.totalTime != 0)
        //    //        excelApp.Cells[row, 11] = (double)svr.totalTime / (double)Trevor.totalTime;
        //    //    else
        //    //        excelApp.Cells[row, 11] = "N/A";
        //    //}
        //    //Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
        //    //row++;

        //    Console.WriteLine("\nnew Algoritem");
        //    Console.WriteLine("----------------");
        //    SuperTree newAlg = new SuperTree(aStart);
        //    watch.Reset();
        //    SuperTree.watchCreateMdd.Reset();
        //    SuperTree.watchSyncronize.Reset();
        //    SuperTree.watchBruteForce.Reset();
        //    watch.Start();
        //    newAlg.solve();
        //    watch.Stop();
        //    excelApp.Cells[row, 1] = instance;
        //    excelApp.Cells[row, 2] = agentsNum;
        //    excelApp.Cells[row, 3] = newAlg.expanded;
        //    excelApp.Cells[row, 4] = SuperTree.numOfFindPath;
        //    excelApp.Cells[row, 5] = SuperTree.watchCreateMdd.ElapsedMilliseconds;
        //    excelApp.Cells[row, 6] = SuperTree.watchSyncronize.ElapsedMilliseconds;
        //    excelApp.Cells[row, 7] = SuperTree.watchBruteForce.ElapsedMilliseconds;
        //    excelApp.Cells[row, 8] = watch.ElapsedMilliseconds;
        //    //excelApp.Cells[row, 9] = "N/A";
        //    //excelApp.Cells[row, 10] = 1;
        //    //if (newAlg.totalCost == -2)
        //    //{
        //    //    excelApp.Cells[row, 7] = "Out of time";
        //    //    excelApp.Cells[row, 10] = 0;
        //    //    excelApp.Cells[row, 11] = "N/A";
        //    //}
        //    //else
        //    //{
        //    //    if (Trevor.totalTime >= 0)
        //    //        excelApp.Cells[row, 11] = (double)newAlg.totalCost / (double)Trevor.totalTime;
        //    //    else
        //    //        excelApp.Cells[row, 11] = "N/A";
        //    //}

        //    Console.WriteLine("total super tree node genreted: {0}", newAlg.expanded);
        //    Console.WriteLine("total brute force: {0}", SuperTree.numOfFindPath);
        //    Console.WriteLine("creata MDD Time In milliseconds: {0}", SuperTree.watchCreateMdd.ElapsedMilliseconds);
        //    Console.WriteLine("syncronize Time In milliseconds: {0}", SuperTree.watchSyncronize.ElapsedMilliseconds);
        //    Console.WriteLine("broute force Time In milliseconds: {0}", SuperTree.watchBruteForce.ElapsedMilliseconds);
        //    Console.WriteLine("total Time In milliseconds: {0}", watch.ElapsedMilliseconds);


        //    row++;

        //    //if (Trevor.totalTime > -1 && newAlg.totalCost > -1 && Trevor.totalTime != newAlg.totalCost)
        //    //{
        //    //    Console.WriteLine("problem");
        //    //    Console.ReadLine();
        //    //}
        //}

        public static void solveGivenProblem(bool[][] grid, AgentState[] aStart)
        {
            int gridSize = grid.Length;
 
            
            watch = new Stopwatch();

            Console.WriteLine("\ntrevor");
            Console.WriteLine("----------------");
            trevor_A_Star = true;
            Trevor t = new Trevor(aStart, false);
            watch.Reset();
            watch.Start();
            t.solve();
            watch.Stop();
            Console.WriteLine("total cost: {0}", Trevor.totalTime);
            Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);

            trevor_A_Star = false;

            Console.WriteLine("A star");
            Console.WriteLine("-------");
                A_star.init_A_Star(aStart, false);
                watch.Start();
                A_star.solve();
                watch.Stop();

            Console.WriteLine("total cost: {0}", A_star.totalTime);
                Console.WriteLine("Time In milliseconds: {0}", watch.ElapsedMilliseconds);
            
          


            Console.WriteLine("\nnew Algoritem");
            Console.WriteLine("----------------");
            BasicIndepandetSubproblemNewAlogoritem newAlg = new BasicIndepandetSubproblemNewAlogoritem(aStart);
            //SuperTree newAlg = new SuperTree(aStart);
            watch.Reset();
            //SuperTree.watchCreateMdd.Reset();
            //SuperTree.watchSyncronize.Reset();
            //SuperTree.watchBruteForce.Reset();
            watch.Start();
            newAlg.solve();
            watch.Stop();

           // Console.WriteLine("total super tree node genreted: {0}", newAlg.expanded);
           // Console.WriteLine("total brute force: {0}", SuperTree.numOfFindPath);
           // Console.WriteLine("creata MDD Time In milliseconds: {0}", SuperTree.watchCreateMdd.ElapsedMilliseconds);
           // Console.WriteLine("syncronize Time In milliseconds: {0}", SuperTree.watchSyncronize.ElapsedMilliseconds);
           // Console.WriteLine("broute force Time In milliseconds: {0}", SuperTree.watchBruteForce.ElapsedMilliseconds);
            Console.WriteLine("total Time In milliseconds: {0}", watch.ElapsedMilliseconds);

        }

        public static bool generateAndSolveWithIndependate(int gridSize, int agentsNum, int obsticalsNum, int instance, Excel.Application excelApp)
        {
            Console.WriteLine("\n\nGrid size - " + gridSize + "X" + gridSize);
            Console.WriteLine("Number Of Obsticals - " + obsticalsNum);
            Console.WriteLine("Number Of Agents - " + agentsNum);
            Console.WriteLine("Instance - " + instance);
            Console.WriteLine("---------------------------");
            excelApp.Cells[row, 4] = instance;
            excelApp.Cells[row, 1] = gridSize + "X" + gridSize;
            excelApp.Cells[row, 2] = obsticalsNum;
            excelApp.Cells[row, 3] = agentsNum;
            //excelApp.WorkbookSync
            watch = new Stopwatch();
            AgentState[] aStart = generateBoard(gridSize, agentsNum, obsticalsNum);


            Console.WriteLine("\ntrevor");
            Console.WriteLine("------");

            trevor_A_Star = true;
            Trevor t = new Trevor(aStart, false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            watch.Reset();
            watch.Start();
            try
            {
                t.solve();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
                excelApp.Cells[row, 13] = e.ToString();
                excelApp.Cells[row, 14] = "Generated"+ Trevor.generated;
                excelApp.Cells[row, 15] = "Expanded" + Trevor.expanded;
               // excelApp.Cells[row, 16] = "Available Virtual Memory = " + Pr];
                return false;
            }
            watch.Stop();
            excelApp.Cells[row, 5] = watch.ElapsedMilliseconds;
            excelApp.Cells[row, 9] = t.min_group;
            excelApp.Cells[row, 10] = t.max_group;
            excelApp.Cells[row, 14] = Trevor.generated;
            excelApp.Cells[row, 15] = Trevor.expanded;

            Console.WriteLine("Trevor total Time In milliseconds: {0}", watch.ElapsedMilliseconds);
            excelApp.Cells[row, 7] = 1;
            if (Trevor.totalTime == -2)
            {
                excelApp.Cells[row, 7] = 0;
            }
            trevor_A_Star = false;

            Console.WriteLine("\nnew Algoritem");
            Console.WriteLine("----------------");
            BasicIndepandetSubproblemNewAlogoritem newAlg = new BasicIndepandetSubproblemNewAlogoritem(aStart);
            watch.Reset();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            watch.Start();
            try
            {
                newAlg.solve();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
                excelApp.Cells[row, 13] = e.ToString();
                // excelApp.Cells[row, 16] = "Available Virtual Memory = " + Pr];
                return false;
            }
            watch.Stop();
            excelApp.Cells[row, 6] = watch.ElapsedMilliseconds;
            excelApp.Cells[row, 8] = 1;
            excelApp.Cells[row, 11] = newAlg.min_group;
            excelApp.Cells[row, 12] = newAlg.max_group;
            if (newAlg.totalCost == -2)
            {
                excelApp.Cells[row, 8] = 0;
            }
            Console.WriteLine("total Time In milliseconds: {0}", watch.ElapsedMilliseconds);
            row++;

            if (Trevor.totalTime > -1 && newAlg.totalCost > -1 && Trevor.totalTime != newAlg.totalCost)
            {
                Console.WriteLine("-------------problem---------------");
                Console.ReadLine();
            }

            return true;
        }
    }
}
